# -*- coding: utf-8 -*-

import commonlib
import workerpool
import datetime, os, traceback
from MongoDbLog import MongoDbLog
from MongoModel import MongoModel

LOG = MongoDbLog("lichtv_itv", 'debug')
MONGO_SERVER = 'beta.mana.vn'    # Server Mana 64bit
MONGO_PORT = 27017


class LichTV(MongoModel):
    
    __dbname = 'lichtv'
    __collection = 'vnexpress'
    __url = 'http://www.itv.vn/Lichphatsong.aspx'
    
    def __init__(self, host='localhost', port=27017):
        MongoModel.__init__(self, host, port)
        self.channels = self.listChannel()
        
    def listChannel(self):
        LOG.debug('start get list channel')
        channels = {}
        try:
            tree = commonlib.getXMLTree(self.__url)
            for ichannel in tree.xpath("//select[@id='comboChannel']/option"):
                channels[ichannel.get('value')] = ichannel.text.strip()
        except:
            LOG.error(traceback.format_exc())
        return channels
    
    def getLichTV(self, channelID):
        LOG.debug('start getLichTV({0})'.format(channelID))
        try:
            now = datetime.datetime.now().strftime("%d/%m/%Y")
            tree = commonlib.getXMLTree(self.__url, {'hdnChannel': channelID, 'hdnDate': now, 'hdnlsAjax': 0})
#            from lxml import etree
#            print etree.tostring(tree, method='html', encoding='utf-8')
            data = []
            print('DEBUG: channel name: %s' % self.channels[channelID])
            for item in tree.xpath("//td[@id='Content']//tr[position()>1]"):
                time = commonlib.getElementText(item.xpath("./td[1]"))
                desc = commonlib.getElementText(item.xpath("./td[2]"))
                if time != '' and desc != '' and desc != '(…)':
                    data.append({'time': time, 'desc': desc})
                    print time, ': ', desc
            db = self.connection[self.__dbname]
            collection = db[self.__collection]
            collection.save({'_id': commonlib.getCRC32Unsign(channelID),
                'name': self.channels[channelID],
                'date': datetime.datetime.now().strftime("%d/%m/%Y"),
                'data': data,
                'lastupdate': datetime.datetime.now()
                })
        except:
            LOG.error(traceback.format_exc())
        
    
if __name__ == '__main__':
    LOG.info('start crawler lichtv itv.vn')
    
    lichtv = LichTV(MONGO_SERVER, MONGO_PORT)
    if len(lichtv.channels) == 0:
        LOG.info('khong lay duoc danh sach cac kenh tai itv.vn. Hay check lai code')
        os._exit(1)
    try:
        pool = workerpool.WorkerPool(size=10)
        pool.map(lichtv.getLichTV, lichtv.channels.keys())
        pool.shutdown()
        pool.wait()
    except:
        LOG.error(traceback.format_exc())
    LOG.info('finished crawler lichtv itv.vn at {0}'.format(datetime.datetime.now()))
    pid = os.getpid()
    os._exit(1)
    os.kill(pid, 9)       # force quit python by pid ~ kill -9 pid
    